package com.xindaoapp.happypet.activity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.protocol.MoccaApi;

@Deprecated
public class BaseLoadAnimationAcitivity extends UMBaseActivity {

    /** 播放加载时动画 */
    protected FrameLayout loading;
    protected ImageView imageView;
    protected static AnimationDrawable animation;
    protected View load_fail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        initLoading();
        super.onStart();
    }

    private void initLoading() {
        loading = (FrameLayout) findViewById(R.id.loading);
        load_fail = findViewById(R.id.ll_load_fail);
        if (load_fail != null)
            load_fail.setVisibility(View.GONE);
        imageView = (ImageView) findViewById(R.id.imageView);
        if (imageView != null) {
            imageView.setBackgroundResource(R.anim.alertdialog_animation);
            animation = (AnimationDrawable) imageView.getBackground();
            animation.setOneShot(false);
            animation.start();
        }
        if (load_fail != null)
            load_fail.setOnClickListener(new onLoadFailClick());
        if (loading != null)
            loading.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
    }

    @SuppressWarnings("unused")
    private boolean isDataArrived = false;

    protected final void onDataArrived(boolean isArrived) {
        if (isArrived) {
            isDataArrived = true;
            if (animation != null && null != imageView) {
                animation.stop();
                if (load_fail != null && load_fail.getVisibility() == View.VISIBLE) {
                    load_fail.setVisibility(View.GONE);
                }
                if (loading != null)
                    loading.setVisibility(View.GONE);
            }
        } else {
            isDataArrived = false;
            if (animation != null && null != imageView) {
                animation.stop();
                if (loading != null && loading.getVisibility() == View.GONE) {
                    loading.setVisibility(View.VISIBLE);
                }
                if (load_fail != null && load_fail.getVisibility() == View.GONE) {
                    load_fail.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    /**
     * 请求成功，但是数据为空，页面显示暂无数据
     */
    protected final void onDataArrivedEmpty() {
        View view = findViewById(R.id.no_data);
        if (null != loading && null != view) {
            loading.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {

                }
            });
        }
    }

    protected final void loading() {
        if (loading != null && loading.getVisibility() == View.GONE) {
            loading.setVisibility(View.VISIBLE);
        }
        if (load_fail != null && load_fail.getVisibility() == View.VISIBLE) {
            load_fail.setVisibility(View.GONE);
        }
        if (animation != null && null != imageView) {
            animation.start();
        }
    }

    private class onLoadFailClick implements OnClickListener {
        @Override
        public void onClick(View v) {
            loading();
            listener.onLoadFail();
        }
    }

    protected void loadFistData() {}

    public void SetOnLoadFailListener(OnLoadFailListener l) {
        listener = l;
    }

    protected final MoccaApi getMoccaApi() {
        return HappyPetApplication.get().getMoccaApi();
    }

    private OnLoadFailListener listener;

    public interface OnLoadFailListener {
        void onLoadFail();
    }

    @Override
    public void onLowMemory() {
        ImageLoader.getInstance().clearMemoryCache();
        System.gc();
        super.onLowMemory();
    }
}
